package library;

import java.util.ArrayList;

import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EncodingUtils;

import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class YonseiManager {
	public static final int INPUT_ERROR = 1;
	public static final int CONNECTION_ERROR = 0;
	public static String htmlContents = "";

	public static ActionClass action;

	public static void getTimeTableText(final WebView wv, final ActionClass actionClass, final String id, final String pw){
		action = actionClass;
		Library.removeSessionCookies();
		WebSettings setting = wv.getSettings();
		setting.setJavaScriptEnabled(true);  //javascript ��� �����ϰ� �Ѵ�
		setting.setPluginsEnabled(true);

		setting.setJavaScriptCanOpenWindowsAutomatically(true);
		YonseiManager y = new YonseiManager();
		MyJavaScriptInterface a = y.new MyJavaScriptInterface();

		wv.addJavascriptInterface(a, "HTMLOUT");	
		wv.setWebViewClient(new WebViewClient(){
			String _url = "";
			boolean isLaunched = false;
			@Override
			public void onPageFinished(WebView view, final String url) {
				// TODO Auto-generated method stub
				if(url.equals(Variables.YONSEI_TIMETABLE_URL)){
				//	wv.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
				}else if(url.startsWith("https://yscec.yonsei.ac.kr/mobile/m_contents_class_announs_list.jsp")){
					view.loadUrl(Variables.YONSEI_TIMETABLE_URL);
//					String postData = username=my_username&password=my_password";
//			                webview.postUrl("url",EncodingUtils.getBytes(postData, "BASE64"));
//				
//					view.postUrl("https://underwood1.yonsei.ac.kr/haksa/SG/sg13_r090_yscec.jsp", EncodingUtils.getBytes("fndhyhg=20122&strRect=start" +
//				      		"&returnPage=sg13_r090.jsp&dbMode=S&master_id=630&sub_id=20", "BASE64"));
				}
				
				super.onPageFinished(view, url);
			}
			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				// TODO Auto-generated method stub
				if(errorCode == ERROR_BAD_URL || errorCode == ERROR_CONNECT || errorCode == ERROR_TIMEOUT || errorCode == ERROR_TOO_MANY_REQUESTS){
					actionClass.actionFailed(CONNECTION_ERROR);
				}else {
					Log.d("Error", errorCode +":::::"+ failingUrl);
				}
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub
				if(url.startsWith(Variables.YONSEI_TOKEN_URL) && !isLaunched)
				{
					_url = url.substring(Variables.YONSEI_TOKEN_URL.length() + 1, url.indexOf("&p_error_code=")); 
					String message = Variables.YONSEI_CERT_URL + _url
							+ "&v=v1.4&locale=&ssousername="
							+ id + "&password=" + pw;

					isLaunched = true;
					view.loadUrl(message);   // 처음 시도하기
					

				}else if(url.startsWith(Variables.YONSEI_TOKEN_URL) && isLaunched){
					actionClass.actionFailed(INPUT_ERROR);
					isLaunched = false;
				}

				return false;
			}
		});
		wv.loadUrl(Variables.YONSEI_LOGINURL);
	}

	


	public static void getCertificate(final WebView wv, final ActionClass actionClass, final String id, final String pw){
		Library.removeSessionCookies();
		WebSettings setting = wv.getSettings();
		setting.setJavaScriptEnabled(true);  //javascript ��� �����ϰ� �Ѵ�

		wv.setWebViewClient(new WebViewClient(){
			String _url = "";
			boolean isLaunched = false;

			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				// TODO Auto-generated method stub
				if(errorCode == ERROR_BAD_URL || errorCode == ERROR_CONNECT || errorCode == ERROR_TIMEOUT || errorCode == ERROR_TOO_MANY_REQUESTS
						|| errorCode == ERROR_UNKNOWN){
					actionClass.actionFailed();
				}
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub
				if(url.startsWith(Variables.YONSEI_TOKEN_URL) && !isLaunched)
				{
					_url = url.substring(Variables.YONSEI_TOKEN_URL.length() + 1, url.indexOf("&p_error_code=")); 
					String message = Variables.YONSEI_CERT_URL + _url
							+ "&v=v1.4&locale=&ssousername="
							+ id + "&password=" + pw;

					isLaunched = true;
					view.loadUrl(message);   // 처음 시도하기

				}else if(url.startsWith(Variables.YONSEI_SUCCESS_URL)){
					actionClass.actionSuccess();
				}else if(url.startsWith(Variables.YONSEI_TOKEN_URL) && isLaunched) {
					actionClass.actionFailed(INPUT_ERROR);
					isLaunched = false;

				}else {
					Log.d("Error", url + "<<곳에서 에러남.");
				}
				return false;
			}
		});

		wv.loadUrl(Variables.YONSEI_LOGINURL);

	}

	public static void startYscecURL(WebView wv, final ActionClass actionClass) {
		wv.getSettings().setJavaScriptEnabled(true);  //javascript ��� �����ϰ� �Ѵ�

		wv.setWebViewClient(new WebViewClient(){

			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				// TODO Auto-generated method stub
				if(errorCode == ERROR_BAD_URL || errorCode == ERROR_CONNECT || errorCode == ERROR_TIMEOUT || errorCode == ERROR_TOO_MANY_REQUESTS){
					actionClass.actionFailed(CONNECTION_ERROR);
				}
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub
				return false;
			}
		});

		wv.loadUrl(Variables.YONSEI_LOGINURL);

	}


	class MyJavaScriptInterface{

		public void showHTML(String html){
			htmlContents = html;
			action.actionSuccess(htmlContents);
		}
	}


}